package com.bwie.controller;

import com.bwie.common.domain.Lotto;
import com.bwie.common.domain.LottoLog;
//import com.bwie.common.domain.SignIn;
import com.bwie.common.domain.Prize;
import com.bwie.common.domain.SignIn;
import com.bwie.common.domain.request.LottoLogListReq;
import com.bwie.common.domain.response.DrawResp;
import com.bwie.common.result.PageResult;
import com.bwie.common.result.Result;
import com.bwie.oss.OssUtil;
import com.bwie.service.LottoService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.List;

/**
 * @className: LottoController
 * @Description: TODO
 * @version: v1.８.0
 * @author: 刘鑫
 * @date: 2025/8/31 17:45
 */
@CrossOrigin
@RestController
@RequestMapping("lotto")
public class LottoController {
    @Autowired
    private LottoService lottoService;

    @Autowired
    private OssUtil ossUtil;

    /**
     * 抽奖列表
     * @param lotto
     * @return
     */
    @PostMapping("lottoList")
    public Result<PageResult<Lotto>> lottoList(@RequestBody Lotto lotto){
        PageHelper.startPage(lotto.getPageNum(),lotto.getPageSize());
        List<Lotto> lottoList=lottoService.lottoList(lotto);
        PageInfo<Lotto> lottoPageInfo = new PageInfo<>(lottoList);
        return PageResult.toResult(lottoPageInfo.getTotal(),lottoPageInfo.getList());
    }

    /**
     * 抽奖记录列表
     * @param lottoLogListReq
     * @return
     */
    @PostMapping("lottoLogList")
    public Result<PageResult<LottoLog>> lottoLogList(@RequestBody LottoLogListReq lottoLogListReq){
        PageHelper.startPage(lottoLogListReq.getPageNum(),lottoLogListReq.getPageSize());
        List<LottoLog> lottoLogList=lottoService.lottoLogList(lottoLogListReq);
        PageInfo<LottoLog> lottoLogPageInfo = new PageInfo<>(lottoLogList);
        return PageResult.toResult(lottoLogPageInfo.getTotal(),lottoLogPageInfo.getList());
    }

    /**
     *上传图片
     * @param file
     * @return
     */
    @PostMapping("upload")
    public Result upload(@RequestBody MultipartFile file){
        String img = ossUtil.uploadMultipartFile(file);
        return Result.success(img);
    }
    @PostMapping("add")
    public Result add(@RequestBody @Valid Lotto lotto){
            lottoService.add(lotto);
             return Result.success();
    }
    @GetMapping("draw/{lottoId}")
    public Result draw(@PathVariable("lottoId") Integer lottoId){
        DrawResp draw = lottoService.draw(lottoId);
        return Result.success(draw);
    }

    @PostMapping("signList")
    public Result<PageResult<SignIn>> signList(@RequestBody SignIn signIn){
        return Result.success( lottoService.signList(signIn));
    }
    @PostMapping("sign")
    public Result sign(){
        DrawResp sign = lottoService.sign();
        return Result.success(sign);
    }
    @GetMapping("findAll")
    public Result<List<Prize>> findAll(){
        List<Prize> all = lottoService.findAll();
        return Result.success(all);
    }




}
